package com.example.utils;

import com.example.framework.ContactData;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ContactDataGenerator {
    private static TestDataGenerator dataGen =  new TestDataGenerator();
    private static Random rnd = new Random();

    public static void main(String[] args) throws IOException {
        if (args.length < 3) {
            System.out.println("Incorrect parameters. Generator has 3 parameters: amount of test data, output file, output file format.");
            return;
        }

        int amount = Integer.parseInt(args[0]);
        File file = new File(args[1]);
        String format = args[2];

        if (file.exists()){
            System.out.println("File " + file + " already exists. Please use another name for file or remove existent file.");
            return;
        }

        List<ContactData> contacts = generateRandomContacts(amount);

        if (format.equals("csv")) {
            saveContactsToCsvFile(contacts, file);
        } else if (format.equals("xml")) {
            saveContactsToXmlFile(contacts, file);
        } else {
            System.out.println("Unknown file format: " + format + ".\nPlease specify correct file format: csv or xml.");
            return;
        }
    }

    private static void saveContactsToXmlFile (List<ContactData> contacts, File file) {
        return;
    }

    private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
        FileWriter writer = new FileWriter(file);
        for (ContactData contact : contacts){
            writer.write(contact.getFirstName() + "," +
                    contact.getLastName() + "," +
                    contact.getAddress() + "," +
                    contact.getHomePhone() + "," +
                    contact.getMobilePhone() + "," +
                    contact.getWorkPhone() + "," +
                    contact.getEmail() + "," +
                    contact.getSecondEmail() + "," +
                    contact.getBirthdayDay() + "," +
                    contact.getBirthdayMonth() + "," +
                    contact.getBirthdayYear() + "," +
                    contact.getSecondAddress() + "," +
                    contact.getSecondHomePhone() + "," +
                    ",#" +
                    "\n");
        }
        writer.close();
    }


    public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
        List<ContactData> list = new ArrayList<ContactData>();
        FileReader reader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line = bufferedReader.readLine();
        while (line != null){
            String[] contactParameters = line.split(",");
            ContactData contact = new ContactData()
                    .withFirstName(contactParameters[0])
                    .withLastName(contactParameters[1])
                    .withAddress(contactParameters[2])
                    .withHomePhone(contactParameters[3])
                    .withMobilePhone(contactParameters[4])
                    .withWorkPhone(contactParameters[5])
                    .withEmail(contactParameters[6])
                    .withSecondEmail(contactParameters[7])
                    .withBirthdayDay(contactParameters[8])
                    .withBirthdayMonth(contactParameters[9])
                    .withBirthdayYear(contactParameters[10])
                    .withSecondAddress(contactParameters[11])
                    .withSecondHomePhone(contactParameters[12]);
            list.add(contact);
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        return list;
    }

    public static List<ContactData> generateRandomContacts (int amount) {
        List<ContactData> list = new ArrayList<ContactData>();

        for (int i = 0; i < amount; i++) {
            ContactData contact = new ContactData()
                    .withFirstName(dataGen.getRandomStringWithRandomLength(7))
                    .withLastName(dataGen.getRandomStringWithRandomLength(15))
                    .withAddress(dataGen.getRandomStringWithRandomLength(45))
                    .withHomePhone(dataGen.getRandomPhone())
                    .withMobilePhone(dataGen.getRandomPhone())
                    .withWorkPhone(dataGen.getRandomPhone())
                    .withEmail(dataGen.getRandomEmail())
                    .withSecondEmail(dataGen.getRandomEmail())
                    .withBirthdayDay(String.valueOf(rnd.nextInt(31) + 1))
                    .withBirthdayMonth(String.valueOf(rnd.nextInt(11) + 1))
                    .withBirthdayYear(dataGen.getRandomYear())
                    .withSecondAddress(dataGen.getRandomStringWithRandomLength(50))
                    .withSecondHomePhone(dataGen.getRandomPhone());
            list.add(contact);
        }
        return list;
    }
}
